package com.qubuyer.business.main

import android.os.Bundle
import android.view.View
import androidx.fragment.app.Fragment
import androidx.viewpager2.adapter.FragmentStateAdapter
import androidx.viewpager2.widget.ViewPager2
import com.qubuyer.R
import com.qubuyer.bean.event.GoToMainEvent
import com.qubuyer.bean.event.GoToMineEvent
import com.qubuyer.bean.event.GoToShopCartEvent
import com.qubuyer.business.category.fragment.CategoryFragment
import com.qubuyer.business.home.fragment.HomeFragment
import com.qubuyer.business.mine.fragment.MineFragment
import com.qubuyer.business.shopcart.fragment.ShopCartFragment
import com.qubuyer.core.app.DataBindingFragment
import com.qubuyer.databinding.FramgntMainBinding
import org.greenrobot.eventbus.Subscribe

class MainFragment : DataBindingFragment<FramgntMainBinding>() {
    private lateinit var tabHomeFragment: HomeFragment
    private lateinit var tabCateFragment: CategoryFragment
    private lateinit var tabCartFragment: ShopCartFragment
    private lateinit var tabMineFragment: MineFragment

    override fun onDataBinding(dataBinding: FramgntMainBinding) {
        dataBinding.host = this
    }

    override val layoutId: Int
        get() = R.layout.framgnt_main


    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        if (savedInstanceState != null) {
            tabHomeFragment = childFragmentManager.getFragment(savedInstanceState, "HomeFragment") as HomeFragment
            tabCateFragment = childFragmentManager.getFragment(savedInstanceState, "CategoryFragment") as CategoryFragment
            tabCartFragment = childFragmentManager.getFragment(savedInstanceState, "ShopCartFragment") as ShopCartFragment
            tabMineFragment = childFragmentManager.getFragment(savedInstanceState, "MineFragment") as MineFragment
        } else {
            tabHomeFragment = HomeFragment()
            tabCateFragment = CategoryFragment()
            tabCartFragment = ShopCartFragment()
            tabMineFragment = MineFragment()
        }
        dataBinding.viewPager.registerOnPageChangeCallback(object : ViewPager2.OnPageChangeCallback() {
            override fun onPageSelected(position: Int) {
                super.onPageSelected(position)
                dataBinding.tvHome.isSelected = position == 0
                dataBinding.tvCate.isSelected = position == 1
                dataBinding.tvCart.isSelected = position == 2
                dataBinding.tvMine.isSelected = position == 3
            }
        })
        dataBinding.viewPager.adapter = object : FragmentStateAdapter(childFragmentManager, lifecycle) {

            override fun getItemCount(): Int {
                return 4
            }

            override fun createFragment(position: Int): Fragment {
                return when (position) {
                    0 -> tabHomeFragment
                    1 -> tabCateFragment
                    2 -> tabCartFragment
                    3 -> tabMineFragment
                    else -> Fragment()
                }
            }
        }
        dataBinding.viewPager.isUserInputEnabled = false
        dataBinding.viewPager.offscreenPageLimit = 3
        dataBinding.viewPager.currentItem = 0
    }

    fun onClick(v: View) {
        when (v.id) {
            R.id.tvHome -> dataBinding.viewPager.setCurrentItem(0, false)
            R.id.tvCate -> {
                dataBinding.viewPager.setCurrentItem(1, false)
            }
            R.id.tvCart -> {
                dataBinding.viewPager.setCurrentItem(2, false)
            }
            R.id.tvMine -> {
                dataBinding.viewPager.setCurrentItem(3, false)
            }
        }
    }

    override fun onSaveInstanceState(outState: Bundle) {
        if (tabHomeFragment.isAdded) {
            childFragmentManager.putFragment(outState, "HomeFragment", tabHomeFragment)
        }
        if (tabCateFragment.isAdded) {
            childFragmentManager.putFragment(outState, "CategoryFragment", tabCateFragment)
        }
        if (tabCartFragment.isAdded) {
            childFragmentManager.putFragment(outState, "ShopCartFragment", tabCartFragment)
        }
        if (tabMineFragment.isAdded) {
            childFragmentManager.putFragment(outState, "MineFragment", tabMineFragment)
        }
        super.onSaveInstanceState(outState)
    }

    @Subscribe
    fun onEventMainThread(event: GoToMainEvent) {
        dataBinding.viewPager.currentItem = 0
    }

    @Subscribe
    fun onEventMainThread(event: GoToShopCartEvent) {
        dataBinding.viewPager.currentItem = 2
    }

    @Subscribe
    fun onEventMainThread(event: GoToMineEvent) {
        dataBinding.viewPager.currentItem = 3
    }
}